package com.ihr360.payroll.service.staff.convertor;

import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.entity.BasicStaffInfoEntity;
import com.ihr360.payroll.entity.staff.CompanySalaryProfileEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileItemsWrapperEntity;
import com.ihr360.payroll.entity.staff.StaffSalaryProfileEntity;
import com.ihr360.payroll.model.dto.salary.convertor.SalaryProfileConvertor;
import com.ihr360.payroll.model.dto.salary.profile.CompanySalaryProfile;
import com.ihr360.payroll.model.dto.staff.StaffSalaryProfile;
import com.ihr360.payroll.model.dto.staff.embeddable.SalaryProfile;
import com.ihr360.payroll.model.dto.staff.embeddable.SalaryProfileItemsWrapper;
import com.ihr360.shared.basicstaffinfo.convertor.SyncedBasicStaffInfoConvertor;
import com.ihr360.shared.basicstaffinfo.dto.SyncedBasicStaffInfo;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StaffSalaryProfileConvertor implements IConvertor<StaffSalaryProfileEntity, StaffSalaryProfile> {


    @Autowired
    private SyncedBasicStaffInfoConvertor syncedBasicStaffInfoConvertor;

    @Autowired
    private SalaryProfileConvertor salaryProfileConvertor;

    @Override
    public StaffSalaryProfile convertEntityToDto(StaffSalaryProfileEntity entity) {
        if (entity == null) {
            return null;
        }
        StaffSalaryProfile dto = new StaffSalaryProfile();
        BasicStaffInfoEntity staffInfoEntity =  entity.getStaff();
        if (staffInfoEntity != null && StringUtils.isNotEmpty(staffInfoEntity.getId())) {
            SyncedBasicStaffInfo staffInfo = new SyncedBasicStaffInfo();

            staffInfo.setId(staffInfoEntity.getId());
            dto.setStaff(staffInfo);
        }
        SalaryProfile salaryProfile = new SalaryProfile();
        dto.setSalaryProfile(salaryProfile);
        dto.setCompanyId(entity.getCompanyId());


        SalaryProfileEntity salaryProfileEntity = entity.getSalaryProfile();
        BeanUtils.copyProperties(salaryProfileEntity,salaryProfile);

        if (salaryProfileEntity != null) {
            SalaryProfileItemsWrapperEntity wrapperEntity = salaryProfileEntity.getWrapper();
            if (wrapperEntity != null && MapUtils.isNotEmpty(wrapperEntity.getItems())) {
                SalaryProfileItemsWrapper wrapper = new SalaryProfileItemsWrapper();
                wrapper.setItems(wrapperEntity.getItems());
                salaryProfile.setWrapper(wrapper);
            }
            CompanySalaryProfileEntity companySalaryProfileEntity =  salaryProfileEntity.getCompanySalaryProfile();
            if (companySalaryProfileEntity != null) {
                CompanySalaryProfile companySalaryProfile = new CompanySalaryProfile();
                companySalaryProfile.setId(companySalaryProfileEntity.getId());
                salaryProfile.setCompanySalaryProfile(companySalaryProfile);
            }
        }
        return dto;
    }

    @Override
    public StaffSalaryProfileEntity convertDtoToEntity(StaffSalaryProfile dto) {

        StaffSalaryProfileEntity entity = new StaffSalaryProfileEntity();
        BeanUtils.copyProperties(dto,entity);
        SyncedBasicStaffInfo staff =  dto.getStaff();
        if (staff != null) {
            BasicStaffInfoEntity staffInfoEntity = syncedBasicStaffInfoConvertor.convertDtoToEntity(staff);
            entity.setStaff(staffInfoEntity);
        }
        SalaryProfile salaryProfile = dto.getSalaryProfile();
        if (salaryProfile != null) {
            SalaryProfileEntity salaryProfileEntity = salaryProfileConvertor.convertDtoToEntity(salaryProfile);
            entity.setSalaryProfile(salaryProfileEntity);
        }
        return entity;
    }


    public List<StaffSalaryProfileEntity> convertDtosToEntitys(List<StaffSalaryProfile> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return Lists.newArrayList();
        }
        List<StaffSalaryProfileEntity> entities =  Lists.newArrayListWithCapacity(dtos.size());
        dtos.forEach(dto -> {
            entities.add(convertDtoToEntity(dto));
        });
        return entities;
    }

    public List<StaffSalaryProfile> convertEntitiesToDtos(List<StaffSalaryProfileEntity> entities) {

        if (CollectionUtils.isEmpty(entities)) {
            return Lists.newArrayList();
        }

        List<StaffSalaryProfile> dtos = Lists.newArrayListWithCapacity(entities.size());
        entities.forEach(entity -> {
            dtos.add(convertEntityToDto(entity));
        });
        return dtos;
    }


}
